package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 */
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>
        implements IWmChannelService {

    /**
     * 根据名称模糊查询分页列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByPage(ChannelDto dto) {
        // 根据名称分页查询频道列表
        // 构建查询条件
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        // 判断传入的频道名称是否为空,不为空的话添加查询条件
        if (!StringUtils.isEmpty(dto.getName())){
            query.like(WmChannel::getName,dto.getName());
        }
        // 构建分页条件
        IPage<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<WmChannel> iPage = page(page, query);
        // 给前端返回通用的分页响应
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),iPage.getTotal(),iPage.getRecords());
        return result;
    }

    /**
     * 新增频道
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult add(WmChannel channel) {
        //判断参数是否为空
        if (channel == null || StringUtils.isEmpty(channel.getName())){
            //返回参数不正确
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断频道名称是否已存在
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName,channel.getName());
        int count = this.count(query);
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在,新增
        channel.setCreatedTime(new Date());
        this.save(channel);
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     *
     * @param channel
     * @return
     */
    @Override
    public ResponseResult put(WmChannel channel) {
        //判断频道名是否已存在
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getName,channel.getName());
        WmChannel channel1 = this.getOne(query);
        //已存在,提示
        if (channel1 != null){

            if (channel.getId() != channel1.getId()){
                //返回错误信息
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
        }
        //更新数据库
        this.updateById(channel);
        //返回响应结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Long id) {
        //判断id是否存在
        LambdaQueryWrapper<WmChannel> query = new LambdaQueryWrapper<>();
        query.eq(WmChannel::getId,id);
        this.remove(query);
        return ResponseResult.okResult();
    }
}
